package main

import (
  "sdl"
  "sdl/ttf"
  //"fmt"
)

type Input struct {
  Base
  text     string
  position int
}

func NewInput(x, y, w, h int) *Input {
  input := new(Input)
  input.x = 200
  input.y = 200
  input.w = 100
  input.h = 30
  return input
}

func (d *Input) getText() string {
  return d.text
}

func (d *Input) keyPressed(key int) {

  if key == 0 {
    return
  }
  
  if key == sdl.K_BACKSPACE {
    if len(d.text) > 0 {
      d.text = d.text[0:len(d.text)-1]
    }
  } else {
    d.text += string(key)
  }
}

func (d *Input) keyReleased(key int) { 
  //d.text += "y"
}

func (d *Input) draw(surface *sdl.Surface) {
  iw := uint16(d.w)
  ih := uint16(d.h)
  
  //fmt.Printf("%p (%T) %p, %p (%T) %p\n", d, d, &d, GetActiveWindow(), GetActiveWindow(), GetActiveWindow())
  
  if (Window)(d) == GetActiveWindow() {
    surface.FillRect(&sdl.Rect{int16(d.x) - 1, int16(d.y) - 1, iw + 2, ih + 2}, 0xff0000)
  }

  surface.FillRect(&sdl.Rect{int16(d.x), int16(d.y), iw, ih}, 100000)
  surface.FillRect(&sdl.Rect{int16(d.x) + 3, int16(d.y) + 3, iw - 6, ih - 6}, 0)
  
  white := sdl.Color{255, 255, 255, 0}
  text := ttf.RenderText_Blended(GetFont(), d.text, white)
  if text == nil { return }
  
  textoffset := int16(text.W) - int16(iw) + 10
  if textoffset < 0 { textoffset = 0 }
  surface.Blit(&sdl.Rect{int16(d.x) + 5, int16(d.y + 5), 0, 0}, text, &sdl.Rect{textoffset, 0, iw - 10, iw - 10})
  text.Free()
  //log.Stderrf("Drawing window %p", d)
}

